package com.tools;

import java.io.FileReader;
import java.io.IOException;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Java4JS {
	
	public static void main(String[] args) throws NoSuchMethodException, ScriptException, IOException {
		System.out.println(excuteJs("基督�?"));
	}
	  /**
     * 调用js文件获取url
     * @param video_id
     * @return
     * @throws ScriptException
     * @throws NoSuchMethodException
	 * @throws IOException 
     */
    public static String excuteJs(String video_id) throws ScriptException,
            NoSuchMethodException, IOException {
        ScriptEngineManager engineManager = new ScriptEngineManager();
        ScriptEngine engine = engineManager.getEngineByName("JavaScript"); // 得到脚本引擎
        String reader = null;
        //获取文件�?在的相对路径
        //String text = System.getProperty("user.dir");
        //reader = text + "\\src\\main\\resources\\test.js";
        String path = Java4JS.class.getResource("/").getPath();
    	reader = path + "/js/baidu_translate.js"; 
    	FileReader fReader = new FileReader(reader);
        engine.eval(fReader );
        
        Invocable inv = (Invocable) engine; 
        //调用js中的方法
        Object test2 = inv.invokeFunction("e", video_id);  
        String url = test2.toString();
        System.out.println(url);
        fReader.close();
        return url;
    }
}
